package net.searchdata.study;

import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

import org.junit.Test;

class Stack<E> {
	private List<E> list = new ArrayList<E>();

	public void push(E e) {
		list.add(e);
	}

	public E pop() {
		return list.remove(list.size() - 1);
	}

	public boolean isEmpty() {
		return list.isEmpty();
	}

	public void pushAll(Collection<? extends E> collection) {
		for (E e : collection) {
			push(e);
		}
	}

	public void popAll(Collection<? super E> collection) {
		while (!isEmpty()) {
			collection.add(pop());
		}
	}
}

public class Entry28 {
	@Test
	public void stackSimpleTest() {
		Stack<Integer> intStack = new Stack<Integer>();

		intStack.push(1);
		intStack.push(2);
		intStack.push(3);

		assertThat(intStack.pop(), is(3));
		assertThat(intStack.pop(), is(2));
		assertThat(intStack.pop(), is(1));

		assertThat(intStack.isEmpty(), is(true));
	}

	@Test
	public void testPushAll() {
		List<Integer> intList = Arrays.asList(1, 2, 3);

		Stack<Number> numberStack = new Stack<Number>();
		numberStack.pushAll(intList);

		assertThat(numberStack.pop().intValue(), is(3));
		assertThat(numberStack.pop().intValue(), is(2));
		assertThat(numberStack.pop().intValue(), is(1));
	}

	@Test
	public void testPopAll() {
		List<Number> numberList = new ArrayList<Number>();

		Stack<Integer> intStack = new Stack<Integer>();
		for (int i = 1; i <= 3; i++) {
			intStack.push(i);
		}
		intStack.popAll(numberList);

		assertThat(numberList.get(0).intValue(), is(3));
		assertThat(numberList.get(1).intValue(), is(2));
		assertThat(numberList.get(2).intValue(), is(1));
	}
}